package fr.inria.arles.temperatureManagement.temperatureManager.service;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cybergarage.upnp.Action;
import org.cybergarage.upnp.Argument;
import org.cybergarage.upnp.Device;
import org.cybergarage.upnp.control.ActionListener;
import org.cybergarage.upnp.device.InvalidDescriptionException;
import fr.inria.arles.temperatureManagement.temperatureManager.ProcessingNodeServer;

/**
 * Represents the Temperature Manager UPnP Device
 * @author ajay chhatwal
 */
public class TemperatureManagerDevice extends Device
        implements ActionListener {
    /**
     * The partition which the Temperature Manager manages.
     */
    public static final String PARTITION = "room 27";
    private final static String DESCRIPTION_FILE_NAME =
            "description/description.xml";

    private ProcessingNodeServer proNode;

     /**
     *
     * Initialises a new Temperature Manager UPnP Device using its descripton
     * files and adds a it as an UPnP action listner.
     * Also starts the Processing Node Server.
     *
     * @throws InvalidDescriptionException if the description file is invalid
     */
    public TemperatureManagerDevice() throws InvalidDescriptionException {
        super(new File(DESCRIPTION_FILE_NAME));
        Action getTempAction = getAction("GetTemp");
        getTempAction.setActionListener(this);
        Action setTempAction = getAction("SetTemp");
        setTempAction.setActionListener(this);
        proNode=new ProcessingNodeServer(PARTITION);
        new Thread(proNode).start();
    }

    /**
     * Processes the action control recieved by the device.
     * @param action The action whose control is recieved
     * @return true if action is carried out sucessfully,false otherwise.
     */
    public boolean actionControlReceived(Action action) {
        System.out.println("Action Control Recieved");
        String actionName = action.getName();

        boolean ret = false;

        if (actionName.equals("GetTemp")) {
            try {
                Argument partitionArg = action.getArgument("Partitions");
                String temp = proNode.getTemp(partitionArg.getValue());
                Argument tempArg = action.getArgument("Temperature");
                tempArg.setValue(temp);
                ret = true;
            } catch (Exception ex) {
                Logger.getLogger(TemperatureManagerDevice.class.getName()).
                        log(Level.SEVERE, null, ex);
            }
        }
        else if (actionName.equals("SetTemp")) {
            try {
                Argument partitionArg = action.getArgument("Partition");
                Argument tempArg = action.getArgument("Temperature");
                proNode.setTemp(partitionArg.getValue(),
                        tempArg.getValue());
                ret = true;
            } catch (Exception ex) {
                Logger.getLogger(TemperatureManagerDevice.class.getName()).
                        log(Level.SEVERE, null, ex);
            }
        }

        return ret;
    }
}
